package cn.dnui_wnx725.dnuifood_16110100725.controller;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.InputType;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.squareup.picasso.Picasso;

import cn.dnui_wnx725.dnuifood_16110100725.R;
import cn.dnui_wnx725.dnuifood_16110100725.Server;
import cn.dnui_wnx725.dnuifood_16110100725.bean.RegistBean;
import cn.dnui_wnx725.dnuifood_16110100725.listener.TListener;
import cn.dnui_wnx725.dnuifood_16110100725.model.CollectFoodModel;


public class FoodDetailActivity extends BaseActivity {
    private ImageView img_food;
    private String foodname,foodintro,foodprice,foodimg,foodid;
    private TextView tv_foodprice,tv_intro;
    private Button btn_buy,btn_favor_food;

    TListener<RegistBean> tListener = new TListener<RegistBean>() {
        @Override
        public void onResponse(RegistBean registBean) {
            showToast("操作成功");
        }

        @Override
        public void onFail(String msg) {
            showToast("操作失败");
        }
    };

    private static final String TAG = "FoodDetailActivity";
    @Override
    void initView() {
        setLayout_file(R.layout.activity_food_detail);
        foodimg=getIntent().getStringExtra("foodimg");
        foodname=getIntent().getStringExtra("foodname");
        foodprice=getIntent().getStringExtra("foodprice");
        foodintro=getIntent().getStringExtra("foodintro");
        foodid=getIntent().getStringExtra("foodid");
        btn_buy=findViewById(R.id.btn_buy);
        tv_foodprice=findViewById(R.id.tv_foodprice);
        tv_intro=findViewById(R.id.tv_intro);
        img_food=findViewById(R.id.img_food);
        Log.d(TAG, "initView: "+Server.BASEIMG+foodimg);
        Glide.with(this).load(Server.BASEIMG+foodimg).into(img_food);
        btn_favor_food=findViewById(R.id.btn_favor_food);

    }

    @Override
    void initEvent() {
        tv_foodprice.setText(foodprice);
        tv_intro.setSingleLine(false);
        tv_intro.setText(foodintro);
        Picasso.with(this).load(foodimg).placeholder(R.drawable.f1).into(img_food);

        btn_buy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                final EditText num=new EditText(FoodDetailActivity.this);
                num.setInputType(InputType.TYPE_CLASS_NUMBER);

//                showToast("不给你买");
                final AlertDialog.Builder builder=new AlertDialog.Builder(FoodDetailActivity.this)
                        .setTitle("请输入购买"+foodname+"的购买数量:")
                        .setView(num)
                        .setPositiveButton("下一步", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                if (num.getText().toString().equals(""))
                                    showToast("请输入数量");
                                else{
                                    AlertDialog.Builder builder1=new AlertDialog.Builder(FoodDetailActivity.this)
                                            .setTitle("请输入地址")
                                            .setView(new EditText(FoodDetailActivity.this))
                                            .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                                @Override
                                                public void onClick(DialogInterface dialogInterface, int i) {
                                                    showToast("下单成功 ");
                                                }
                                            })
                                            .setNegativeButton("取消",null);
                                    builder1.show();
                                }
                            }
                        })
                        .setNegativeButton("取消" ,  null );
                builder.show();
            }
        });
    }

    @Override
    void initData() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_food_detail);
//        Intent intent=getIntent();
        initView();
        initEvent();
        initData();
        btn_favor_food.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                CollectFoodModel collectFoodModel = new CollectFoodModel();
                collectFoodModel.collect(getUserID(),foodid,tListener);
            }
        });
    }
}